//
//  ApplicationUtils.m
//  FMSManagerDemo
//
//  Created by hongbao.cui on 14-11-11.
//  Copyright (c) 2014年 hongbao.cui. All rights reserved.
//

#import "ApplicationUtils.h"
#import "FMSMBProgressHUD.h"
#import "BDKNotifyHUD.h"

@implementation ApplicationUtils
static FMSMBProgressHUD* hud0;
//网络正在加载的提示
+(void)promptRotateWithViewTitle:(NSString*)title showView:(UIView*)viewSelf {
    if (hud0) {
        [hud0 removeFromSuperview];
        hud0 = nil;
    }
    if (viewSelf) {
        hud0 = [FMSMBProgressHUD showHUDAddedTo:viewSelf animated:YES];
    }
    
    [hud0 show:YES];
    //    hud0.dimBackground = YES;//暗淡的背景
    hud0.mode = MBProgressHUDModeIndeterminate;
    hud0.labelText = title;
}
//文字提示
+(void)promptWithTitle:(NSString*)title showView:(UIView*)viewSelf
{
    if (hud0) {
        [hud0 removeFromSuperview];
        hud0 = nil;
    }
    if (viewSelf) {
        hud0 = [FMSMBProgressHUD showHUDAddedTo:viewSelf animated:YES];
    }
    [hud0 show:YES];
    
    //    hud0.dimBackground = YES;//暗淡的背景
    hud0.mode = MBProgressHUDModeText;
    hud0.labelText = title;
    //隐藏提示器
    [hud0 hide:YES afterDelay:3];
}

//成功的提示
+(void)promptFinishWithTitle:(NSString *)title showView:(UIView*)viewSelf
{
    if (hud0) {
        [hud0 removeFromSuperview];
        hud0 = nil;
    }
    if (viewSelf) {
        hud0 = [FMSMBProgressHUD showHUDAddedTo:viewSelf animated:YES];
    }
    [hud0 show:YES];
    hud0.customView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"progressHUD_yes.png"]];
    hud0.mode = MBProgressHUDModeCustomView;
    hud0.labelText = title;
    //  隐藏提示器
    [hud0 hide:YES afterDelay:1];
}
//失败提示
+(void)promptFailWithTitle:(NSString*)title showView:(UIView*)viewSelf
{
    if (hud0) {
        [hud0 removeFromSuperview];
        hud0 = nil;
    }
    if (viewSelf) {
        hud0 = [FMSMBProgressHUD showHUDAddedTo:viewSelf animated:YES];
    }
    [hud0 show:YES];
    hud0.customView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"progressHUD_no.png"]];
    hud0.mode = MBProgressHUDModeCustomView;
    hud0.labelText = title;
    //  隐藏提示器
    [hud0 hide:YES afterDelay:1];
}

//移除提示器
+(void)promptRemove {
    [hud0 hide:YES afterDelay:.5];
}
+(void)showAlert:(NSString *)msg Delegate:(id)object{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:object cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}
+(void)showAlertMSG:(NSString *)msg{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
}
+ (UIColor *) stringTOColor:(NSString *)str {
    NSString *cString = [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];//字符串处理
    //例子，stringToConvert #ffffff
    if ([cString length] < 6)
        return [UIColor whiteColor];//如果非十六进制，返回白色
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];//去掉头
    if ([cString length] != 6)//去头非十六进制，返回白色
        return [UIColor whiteColor];
    //分别取RGB的值
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    unsigned int r, g, b;
    //NSScanner把扫描出的制定的字符串转换成Int类型
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    //转换为UIColor
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}
+(UIImage *)getLocalImage:(NSString *)imageName{
    NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
    UIImage *imge = [UIImage imageWithContentsOfFile:path];
    return imge;
}
+(BOOL)isFileExitsAtPath:(NSString *)filePath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]) {
        return YES;
    }
    return NO;
}
+(BOOL)deleteExitsFile:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL success = NO;
    NSError *error = nil;
    success = [fileManager removeItemAtPath:filePath error:&error];
    return success;
}
+(void)deleteExitsFileAtPath:(NSString *)fileInPath suffix:(NSString *)extension{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *contents = [fileManager contentsOfDirectoryAtPath:fileInPath error:NULL];
    NSEnumerator *e = [contents objectEnumerator];
    NSString *filename;
    while ((filename = [e nextObject])) {
        if ([[filename pathExtension] isEqualToString:extension]) {
            [fileManager removeItemAtPath:[fileInPath stringByAppendingPathComponent:filename] error:NULL];
        }
    }
}
+(void)makeToast:(NSString *)title withImageName:(NSString *)imageName withView:(UIView *)aView{
    BDKNotifyHUD  *_notify;
    if (!_notify) {
        _notify = [BDKNotifyHUD notifyHUDWithImage:[ApplicationUtils getLocalImage:imageName] text:title];
        _notify.center = CGPointMake(aView.center.x, aView.center.y - 20-44-45);
    }
    [aView addSubview:_notify];
    if (_notify.isAnimating) return;
    [_notify presentWithDuration:1.0f speed:0.5f inView:aView completion:^{
        [_notify removeFromSuperview];
    }];
}
+(UIImage *)imageWithGaussianBlur9:(UIImage *)image {
    float weight[5] = {0.1270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162};
    // Blur horizontally
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height) blendMode:kCGBlendModeNormal alpha:weight[0]];
    for (int x = 1; x < 5; ++x) {
        [image drawInRect:CGRectMake(x, 0, image.size.width, image.size.height) blendMode:kCGBlendModeNormal alpha:weight[x]];
        [image drawInRect:CGRectMake(-x, 0, image.size.width, image.size.height) blendMode:kCGBlendModeNormal alpha:weight[x]];
    }
    UIImage *horizBlurredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // Blur vertically
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    [horizBlurredImage drawInRect:CGRectMake(0, 0, image.size.width, image.size.height) blendMode:kCGBlendModeNormal alpha:weight[0]];
    for (int y = 1; y < 5; ++y) {
        [horizBlurredImage drawInRect:CGRectMake(0, y, image.size.width, image.size.height) blendMode:kCGBlendModeNormal alpha:weight[y]];
        [horizBlurredImage drawInRect:CGRectMake(0, -y, image.size.width, image.size.height) blendMode:kCGBlendModeNormal alpha:weight[y]];
    }
    UIImage *blurredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //
    return blurredImage;
}
+(NSString*)jsonValue:(id)value{
    
    NSError *error = nil;
    BOOL success = [NSJSONSerialization isValidJSONObject:value];
    if (success) {
        NSData *data = [NSJSONSerialization dataWithJSONObject:value
                                                       options:0 // non-pretty printing
                                                         error:&error];
        if(error)
            FMSCHBLog(@"JSON Parsing Error: %@", error);
        return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    }
    FMSCHBLog(@"inValidJSONObject Error");
    return nil;
}

/*!
 * @brief 把格式化的JSON格式的字符串转换成字典
 * @param jsonString JSON格式的字符串
 * @return 返回字典,或是数组
 */
+(id)objectFromJson:(NSString *)jsonString{
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    id dicOrArray = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        FMSCHBLog(@"json解析失败：%@",err);
        return nil;
    }
    return dicOrArray;
}

#pragma mark - 新增，用户答案存储
// 存储
+(void)saveUserAnswersToLocalPathOfPaper: (NSArray*)paperData{
    
}

// 读取
+(void)readLocalUserAnswersOfPaper: (NSArray*)paperData{
    
}


@end
